package com.demo.client;

import com.demo.bean.PositionRequest;
import com.demo.bean.PositionVO;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class JerseyClientTest {

    private Configuration clientConfig;
    @BeforeEach
    public void setup() {
        clientConfig = new ClientConfig()
                .property("jersey.clientConfig.client.connectTimeout", 1000)
                .property("jersey.clientConfig.client.readTimeout", 1000);
    }

    /**
     * 测试Jersey GET请求
     */
    @Test
    public void testGet() {
        try (Client client = ClientBuilder.newClient(clientConfig)) {
            WebTarget webTarget = client.target("http://localhost:8080/").path("jersey/get_with_query");
            Response response = webTarget.queryParam("location", "san jose")
                    .request().header("Authorization", "BEAR xxx").get();
            String result = response.readEntity(String.class);
            System.out.println(result);
            assertEquals("query param location: san jose", result);
        }
    }

    /**
     * 测试Jersey POST请求
     */
    @Test
    public void testPost1() {
        PositionRequest positionRequest = PositionRequest.builder().posId("0").posName("开机广告位").build();
        Entity<PositionRequest> entity = Entity.json(positionRequest);
        basePost(entity);
    }

    @Test
    public void testPost2() {
        String jsonData = "{\"posId\":\"1\",\"posName\":\"声音贴片广告位\"}";
        Entity<String> entity = Entity.entity(jsonData, MediaType.APPLICATION_JSON);
        basePost(entity);
    }

    @Test
    public void testPost3() {
        String jsonData = "{\"posId\":\"1\",\"posName\":\"声音贴片广告位\"}";
        Entity<String> entity = Entity.entity(jsonData, MediaType.APPLICATION_JSON);

        try (Client client = ClientBuilder.newClient(clientConfig)) {
            WebTarget webTarget = client.target("http://localhost:8080/").path("jersey/adList");
            Response response = webTarget.request()
                    .header("Authorization", "BEAR xxx")
                    .post(entity);
            GenericType<List<PositionVO>> genericType = new GenericType<>() {};
            List<PositionVO> list = response.readEntity(genericType);
            System.out.println(list);
        }
    }

    private void basePost(Entity<?> entity) {
        try (Client client = ClientBuilder.newClient(clientConfig)) {
            WebTarget webTarget = client.target("http://localhost:8080/").path("jersey/ads");
            Response response = webTarget.request()
                    .header("Authorization", "BEAR xxx")
                    .post(entity);

            PositionVO vo = response.readEntity(PositionVO.class);
            System.out.println(vo);
        }
    }

}
